﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    public partial class TAMZLAN
    {

        public TAMZLAN()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VAMSTAMM();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                AMTYP = "";
                AMART = "";
                AMKST = "";
                AMCD = "";
            }
            else
            {
                AMTYP = TMV.AMTYP;
                AMART = TMV.AMART;
                AMKST = TMV.AMKSTKRS + AMKST;
                AMCD = TMV.AMVERFKZ;
                if (TMV.AMSTDVAR == "J")
                    IsSTD = true;
                else
                    IsSTD = false;
            }
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {

            TBMain.Clear();
            dwData = TMV.TBMain.NewRow();
            dwData["AMMANDANT"] = app.Mandant;
            dwData["AMNRKRS"] = " AND AMNRKRS LIKE '" + KRS.Trim() + "%'";
            dwData["AMLFDNR"] = " AND AMLFDNR LIKE '" + LFD.Trim() + "%'";
            dwData["AMPOS"] = " AND AMPOS LIKE '" + POS.Trim() + "%'";
            dwData["AMART"] = " AND AMART LIKE 'ZLAN%'";
            SResult = TMV.QAM03(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {

            try
            {

                if (!IsNew)
                {
                    IsNew = true;
                    AMTYP = "";
                    AMKST = "";
                    AMART = "";
                    LFD = "";
                    return;
                }
                if (AMTYP.Trim().Length < 2)
                {
                    throw new Exception("必需输入类型");

                }
                if (AMKST.Trim().Length < 2)
                {
                    throw new Exception("必须输入成本中心");

                }
                if (AMART.Trim().Length < 2)
                {
                    throw new Exception("必需输入种类");

                }

                if (LFD.Trim().Length < 2)
                {
                    throw new Exception("必需输入编号");

                }
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData);
                dwData["AMMANDANT"] = app.Mandant; 
                dwData["AMTYP"] = AMTYP;
                dwData["AMART"] = AMART;
                dwData["AMKSTKRS"] = AMKST.Substring(0, 2);
                dwData["AMKST"] = AMKST.Substring(2, 4);
                dwData["AMPKSTKRS"] = dwData["AMKSTKRS"];
                dwData["AMPKST"] = dwData["AMKST"];
               
                dwData["AMNRKRS"] = KRS.PadRight(2);
                dwData["AMLFDNR"] = LFD.PadRight(4);
                dwData["AMPOS"] =Helpers.Helper.Right("00"+POS,2);
                Insert(dwData);
                vwMaster.View.MoveCurrentToFirst();

                IsNew = false;
                AMTYP = "";
                AMKST = "";
                AMART = "";
                LFD = "";
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DAM01(dwMaster);
            base.DeleteCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.UpdateAMSTAMM(dwMaster, 0);
            base.SaveCommand(sender, e);
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {

        }


        public override void OnLOChg(string lo)
        {
            if (lo.Length < 2)
                return;
            if (!IsEdit)
                return;
            TMV.AMLO = lo;
            base.OnLOChg(lo);
        }
        public override void OnLBChg(string lb)
        {
            if (lb.Length < 2)
                return;
            if (!IsEdit)
                return;
            TMV.AMLB = lb;
            base.OnLBChg(lb);
        }
        public override void OnSTDChg(bool value)
        {
            if (!IsEdit)
                return;
            if (IsSTD)
                TMV.AMSTDVAR = "J";
            else
                TMV.AMSTDVAR = "N";
            base.OnSTDChg(value);
        }
        public override void OnLPChg(string lp)
        {
            if (lp.Length < 8)
                return;
            if (!IsEdit)
                return;
            TMV.AMLPLATZ = lp;
            base.OnLPChg(lp);
        }
        public override void OnAMARTChg(string value)
        {
            if (value.Length < 8)
                return;
            if (!IsEdit)
                return;
            TMV.AMART = value;
            base.OnAMARTChg(value);
        }
        public override void OnAMTYPChg(string value)
        {
            if (value.Length < 8)
                return;
            if (!IsEdit)
                return;

            TMV.AMTYP = value;
            base.OnAMTYPChg(value);
        }
        public override void OnLIEFNRChg(string value)
        {
            if (!IsEdit)
                return;
            if (value.Length < 8)
                return;
            TMV.AMLIEFNR = value;
            base.OnLIEFNRChg(value);
        }
        public override void OnAMKSTChg(string value)
        {
            if (value.Length < 6)
                return;
            if (!IsEdit)
                return;

            TMV.AMKSTKRS = value.Substring(0, 2);
            TMV.AMKST = value.Substring(2, 4);
            TMV.AMPKSTKRS = TMV.AMKSTKRS;
            TMV.AMPKST = TMV.AMKST;
            base.OnAMKSTChg(value);
        }
        public override void OnAMCDChg(string value)
        {
            if (value.Length < 1)
                return;
            if (!IsEdit)
                return;
            TMV.AMVERFKZ = AMCD;
            base.OnAMCDChg(value);
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            TMV.CHKZLAM(EXAUF);
            vwMaster.View.MoveCurrentToFirst();
            base.OnEXAUFChg(vlaue);
        }

        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM != KEY)
                return;
            if (KEY.Length == 0)
                return;
            dwData = TBMain.NewRow();
            dwData["AMMANDANT"] = app.Mandant;
            dwData["AMNRKRS"] = "AND AMNRKRS= '" + ORDERNUM.Substring(0, 2) + "'";
            dwData["AMLFDNR"] = "AND AMLFDNR= '" + ORDERNUM.Substring(2, 4) + "'";
            dwData["AMPOS"] = "AND AMPOS= '" + ORDERNUM.Substring(6, 2) + "'";
            SResult = TMV.QAM03(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            base.LOADBYAUF();
        }
    }
}
